package cn.mumzone.pos.ui.adapter;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatImageView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import cn.mumzone.pos.R;
import cn.mumzone.pos.model.HelpCenterChildEntity;
import cn.mumzone.pos.ui.widgets.AnimatedExpandableListView;

/**
 * Created by JoeLjt on 2019/11/18.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class ADA_HelpCenter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {

    private Context context;
    private LayoutInflater mInflater;
    private List<String> expandableListTitle;
    private Map<String, List<HelpCenterChildEntity>> expandableListDetail;

    private AnimatedExpandableListView mListView;

    private int lastExpandedGroupPosition = -1;

    private ChildHolder mSelectedChildHolder;
    private HelpCenterChildEntity mSelectedChildEntity;

    public ADA_HelpCenter(Context context, AnimatedExpandableListView listView) {
        this.context = context;
        this.mListView = listView;
        mInflater = LayoutInflater.from(context);
    }

    public void update(List<String> keyList, Map<String, List<HelpCenterChildEntity>> map) {
        this.expandableListTitle = keyList;
        this.expandableListDetail = map;
        notifyDataSetChanged();
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        if (expandableListDetail == null) return null;
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getRealChildView(int listPosition, final int expandedListPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
        final HelpCenterChildEntity childEntity = (HelpCenterChildEntity) getChild(listPosition, expandedListPosition);

        final ChildHolder mChildHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_help_center_child, null);
            mChildHolder = new ChildHolder(convertView);
            convertView.setTag(mChildHolder);
        } else {
            mChildHolder = (ChildHolder) convertView.getTag();
        }

        mChildHolder.tvChildTitle.setText(childEntity.getChildTitle());

        if (childEntity.isSelected()) {
            mChildHolder.vSelected.setVisibility(View.VISIBLE);
            mChildHolder.itemView.setBackgroundColor(Color.parseColor("#F0F0F0"));
        } else {
            mChildHolder.vSelected.setVisibility(View.INVISIBLE);
            mChildHolder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }

        mChildHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mChildHolder == mSelectedChildHolder) {
                    return;
                }

                if (mSelectedChildEntity == null) {
                    childEntity.setSelected(true);
                    mSelectedChildEntity = childEntity;
                    if (mOnSelectedListener != null) {
                        mOnSelectedListener.onChildSelected(childEntity.getChildContent());
                    }
                    notifyDataSetChanged();
                    return;
                }

                mSelectedChildEntity.setSelected(false);
                childEntity.setSelected(true);
                mSelectedChildEntity = childEntity;

                if (mOnSelectedListener != null) {
                    mOnSelectedListener.onChildSelected(childEntity.getChildContent());
                }

                notifyDataSetChanged();

            }
        });
        return convertView;
    }

    @Override
    public int getRealChildrenCount(int listPosition) {
        if (expandableListDetail == null) return 0;
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        if (expandableListTitle == null) return null;
        return this.expandableListTitle.get(listPosition);
    }

    @Override
    public int getGroupCount() {
        if (expandableListTitle == null) return 0;
        return this.expandableListTitle.size();
    }

    @Override
    public long getGroupId(int listPosition) {
        return listPosition;
    }

    @Override
    public View getGroupView(final int listPosition, final boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String listTitle = (String) getGroup(listPosition);
        GroupHolder mGroupHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_help_center_group, null);
            mGroupHolder = new GroupHolder(convertView);
            convertView.setTag(mGroupHolder);
        } else {
            mGroupHolder = (GroupHolder) convertView.getTag();
        }


        mGroupHolder.tvTitle.setTypeface(null, Typeface.BOLD);
        mGroupHolder.tvTitle.setText(listTitle);

        mGroupHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isExpanded) {
                    mListView.collapseGroupWithAnimation(listPosition);
//                    onGroupCollapsed(v);
                    lastExpandedGroupPosition = -1;
//                    mPreView = null;
                } else {
                    if (getLastExpandedGroupPosition() != -1) {
                        mListView.collapseGroupWithAnimation(getLastExpandedGroupPosition());
//                        onGroupCollapsed(mPreView);
                    }

                    mListView.expandGroupWithAnimation(listPosition);
//                    onGroupExpanded(v);

                    lastExpandedGroupPosition = listPosition;
//                    mPreView = v;

                }

            }
        });

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }

    public void onGroupCollapsed(View arrow) {
        ObjectAnimator rotate = ObjectAnimator.ofFloat(arrow.findViewById(R.id.iv_arrow),
                "rotation", 180f, 0f).setDuration(200);
        rotate.setInterpolator(new LinearInterpolator());
        rotate.start();
    }

    public void onGroupExpanded(View arrow) {
        ObjectAnimator rotate = ObjectAnimator.ofFloat(arrow.findViewById(R.id.iv_arrow),
                "rotation", 0f, -180f).setDuration(200);
        rotate.setInterpolator(new LinearInterpolator());
        rotate.start();
    }

    public int getLastExpandedGroupPosition() {
        return lastExpandedGroupPosition;
    }

    class GroupHolder {

        private View itemView;
        private TextView tvTitle;
        private AppCompatImageView ivArrow;

        public GroupHolder(View itemView) {
            this.itemView = itemView;
            tvTitle = itemView.findViewById(R.id.group_title);
            ivArrow = itemView.findViewById(R.id.iv_arrow);
        }
    }

    class ChildHolder {

        private View itemView;
        private View vSelected;
        private TextView tvChildTitle;

        public ChildHolder(View itemView) {
            this.itemView = itemView;
            vSelected = itemView.findViewById(R.id.v_selected);
            tvChildTitle = itemView.findViewById(R.id.child_title);
        }
    }

    public interface OnSelectedListener{
        void onChildSelected(String content);
    }

    private OnSelectedListener mOnSelectedListener;

    public void setOnSelectedListener(OnSelectedListener mListener) {
        this.mOnSelectedListener = mListener;
    }
}
